package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.PageBean;
import com.woniuxy.entity.Student;
import com.woniuxy.mapper.MyBatisUtil;
import com.woniuxy.mapper.StudentMapper;
import com.woniuxy.service.StudentService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author 作者
 * @create 2025-06-19 15:43
 */
public class StudentServiceImpl implements StudentService {
    @Override
    public PageBean<Student> getPager(Student student,int pageIndex,int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        //调用Mapper获取本页数据的集合
        List<Student> students = studentMapper.selectConditionData(student, pageIndex, pageSize);
        //调用Mapper获取总记录数
        int totalRecord = studentMapper.selectConditionCount(student);
        //返回一个完整的分页数据PageBean
        PageBean<Student> pageBean = new PageBean<>();
        //根据方法参数获取
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        //数据库查询
        pageBean.setData(students);
        pageBean.setTotalRecord(totalRecord);
        //总页数:根据总记录数和每页记录数计算
        pageBean.setTotalPage(totalRecord%pageSize==0?totalRecord/pageSize:totalRecord/pageSize+1);
        return pageBean;
    }

    @Override
    public PageBean<Student> getPagerPlugin(Student student, int pageIndex, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        PageHelper.startPage(pageIndex,pageSize);
        List<Student> students = studentMapper.selectPagerByPlugin(student);
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        PageBean<Student> pageBean = new PageBean<>();
        pageBean.setData(students);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord((int) pageInfo.getTotal());
        pageBean.setTotalPage(pageInfo.getPages());
        return pageBean;
    }
}
